using Newtonsoft.Json;
using Map;

namespace immortal_c_.map
{
    class MapInitializer
    {

        static public Dictionary<string, BaseMap> InitializeMaps()
        {

            BaseMap map;
            Dictionary<string, BaseMap> maps = [];
            string rootPath = MainServer.Server.rootPath;
            string[] files;
            try
            {
                files = Directory.GetFiles($"{rootPath}\\map", "*.json", SearchOption.AllDirectories);
                foreach (string file in files)
                {
                    string JsonMap = File.ReadAllText(file);
                    map = JsonConvert.DeserializeObject<BaseMap>(JsonMap, new JsonSerializerSettings
                    {
                        TypeNameHandling = TypeNameHandling.Auto,
                    }) ?? new();
                    maps[map.Name] = map;
                }
            }
            catch (DirectoryNotFoundException)
            {
                Directory.CreateDirectory($"{rootPath}\\map");
            }
            return maps;
        }
    }
}